package com.team.arms.yuding.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.yuding.domain.ConditionalCandidate;
import com.team.arms.yuding.domain.VoluntaryInformation;
import com.team.arms.yuding.domain.dto.ConditionalCandidateDto;
import com.team.arms.yuding.domain.vo.VoluntaryInformationVo;
import com.team.arms.yuding.mapper.ConditionalCandidateMapper;
import com.team.arms.yuding.service.ConditionalCandidateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 
 * @TableName y_conditional_candidate
 */
@Api( tags = "条件兵选人")
@RestController
@RequestMapping("/reservesoldiers/ConditionalCandidate")
public class ConditionalCandidateController extends BaseController {

    @Autowired
    private ConditionalCandidateService conditionalCandidateService;

    @Autowired
    private ConditionalCandidateMapper conditionalCandidateMapper;

    /**
     * 查询条件兵选人信息
     * @return
     */
    @ApiOperation("查询条件兵选人信息")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ConditionalCandidate> list = conditionalCandidateService.list();
        PageInfo<ConditionalCandidate> pageInfo = new PageInfo<ConditionalCandidate>(list);
        PageVO<ConditionalCandidate> candidatePageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, candidatePageVO);
        candidatePageVO.setResult(pageInfo.getList());
        candidatePageVO.setPage(pageNum);
        return AjaxResult.success(candidatePageVO);
    }
    /**
     *
     * @param field
     * @param content
     * @return
     */
//    @ApiOperation("根据对应字段查询条件兵选人信息")
//    @GetMapping("/selectByFile")
//    public AjaxResult selectByName( @ApiParam("查询的字段") @RequestParam(value = "field") String field ,
//                                    @ApiParam("查询的内容") @RequestParam(value = "content")String content){
//        QueryWrapper<ConditionalCandidate> queryWrapper = new QueryWrapper<>();
//        if (field.equals("部队名称")) {
//            queryWrapper.like("troop_name", content);
//        }
//        if (field.equals("兵种名称")) {
//            queryWrapper.like("soldier_info_name", content);
//        }
//        if (field.equals("年度")) {
//            queryWrapper.eq("year", content);
//        }
//        if (field.equals("批次")) {
//            queryWrapper.eq("batch", content);
//        }
//            return AjaxResult.success(conditionalCandidateMapper.selectList(queryWrapper));
//    }
    @ApiOperation("根据对应字段查询条件兵选人信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField( @ApiParam("查询的字段") @RequestParam(value = "field") String field ,
                                   @ApiParam("查询的内容") @RequestParam(value = "content")String content){
        return AjaxResult.success(conditionalCandidateMapper.selectByFiled(field,content));
    }

    /**
     * 输入选人人数
     * @param candidatesNumber
     * @return
     */
    @ApiOperation("输入选人人数")
    @PutMapping("/update")
    public AjaxResult updateConditions(@ApiParam("条件兵选人id")@RequestParam(value = "conditional_candidate_id")String id,
                                       @ApiParam("选人人数")@RequestParam(value = "candidates_number") String candidatesNumber){
        UpdateWrapper<ConditionalCandidate> wrapper = new UpdateWrapper<>();
        wrapper.eq("conditional_candidate_id",id).set("candidates_number",candidatesNumber);
        return toAjax(conditionalCandidateService.update(null,wrapper));
    }

    /**
     * 根据条件查询条件兵选人信息
     * @param conditionalCandidateDto
     * @return
     */
    @ApiOperation("根据条件查询条件兵选人信息")
    @PostMapping("/selectConditions")
    public TableDataInfo selectConditions(
            @RequestBody(required = false) ConditionalCandidateDto conditionalCandidateDto){

        startPage();

        List<ConditionalCandidate> list = conditionalCandidateService.selectConditions(conditionalCandidateDto);
        return getDataTable(list);
    }

    /**
     * 导出条件查询条件兵选人信息
     */
    @ApiOperation("导出条件查询条件兵选人信息")
    @Log(title = "条件查询条件兵选人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<ConditionalCandidate> list = conditionalCandidateService.list();        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), VoluntaryInformation.class).sheet("条件查询条件兵选人信息")
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}